< previous page page_261 next page >

Page 261
and extra padding is added into the structure to enforce this alignment rule. This means the structure that Visual Basic passes to the SHFileOperation function is as shown in Figure S30-3.
0261-01.gif
Figure S30-2:
Expected layout of the SHFILEOPSTRUCT structure in memory
The fAnyOperationsAborted, hNameMappings, and lpszProgressTitle fields appear in the wrong place in memory! To prove this, try the program again, but this time do not set the lpszProgressTitle field (delete the line where it is set to a string).
The function now works! Well, it works in the sense that the file is copied and the function does not crash when the program is closed. It still doesn't display a string.
0261-02.gif
Figure S30-3:
Actual layout of the SHFILEOPSTRUCT structure as passed to the 
SHFileOperation function
The solution to this problem is simple in conceptyou need to somehow compress the structure to eliminate the padding before it is passed to the API function, as shown in Figure S30-4.
0261-03.gif
Figure S30-4:
Shifting data within the SHFILEOPSTRUCT structure to achieve
single-byte packing
The easiest way to do this is to use software that is designed specifically to do UDT packing (user-defined type packing), such as Desaware's SpyWorks package. With this approach, you define the structure fields for the UDT packing program and pass it the structure. The software packs it into a single-byte packed memory buffer and returns a memory buffer you can pass to the API function. It then unpacks the data back into your structure after the function call returns.
This kind of software is especially useful when you have multiple misalignments in a structure. When there is only one misalignment, as you have here, you can perform this operation yourself.

 
< previous page page_261 next page >